package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.conn.GetSoftwareUpdate;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.widget.UpdateService;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/2/14.
 */
public class AboutSoftwareActivity extends JTActivity implements View.OnClickListener {
    private TextView title_name;

    private LinearLayout ll_copyright_having, ll_software_update, ll_company_introduce,ll_copyright;

    private TextView tv_version;

    public static UpdateSoftware updateSoftware;

    private String message = "";

    private TipDialog mDialog;

    private String DownloadPath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_about);
        initView();

    }

    private void initView() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("关于软件");

        (ll_company_introduce = (LinearLayout) findViewById(R.id.id_company_introduce)).setOnClickListener(this);

        (ll_copyright_having = (LinearLayout) findViewById(R.id.id_copyright_having)).setOnClickListener(this);

        (ll_software_update = (LinearLayout) findViewById(R.id.id_software_update)).setOnClickListener(this);

        (ll_copyright = (LinearLayout) findViewById(R.id.ll_copyright)).setOnClickListener(this);

        (tv_version = (TextView) findViewById(R.id.tv_version)).setText("当前版本号" + UtilApp.versionName(context));

        updateSoftware = new UpdateSoftware() {
            @Override
            public void update() {

                if(message.equals("提示版本升级")){

                    if(!DownloadPath.equals("")){

                        //更新版本
                        Intent intent=new Intent(AboutSoftwareActivity.this, UpdateService.class);

                        intent.putExtra("file_real_name", UtilApp.appName(context)+".apk");

                        intent.putExtra("title", UtilApp.appName(context));

                        intent.putExtra("DownloadPath", DownloadPath);

                        startService(intent);

                        UtilToast.show(context, "正在后台下载，稍后会自动安装");


                    }

                    mDialog.dismiss();

                }else {

                    mDialog.dismiss();

                }

            }
        };

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_company_introduce:

//                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(AboutSoftwareActivity.this, CompanyIntroduceActivity.class));

                break;
            case R.id.id_copyright_having:

//                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();

                break;
            case R.id.id_software_update:

                //获得版本号

                String androidVersion = UtilApp.versionName(context);

                new GetSoftwareUpdate(androidVersion, new AsyCallBack<GetSoftwareUpdate.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, GetSoftwareUpdate.Info info) throws Exception {

                        if(toast.equals("成功")){

                            message = info.msg;

                            DownloadPath = info.DownloadPath;

                            mDialog = new TipDialog(context);

                            if(message.equals("提示版本升级")){

                                mDialog.setTip("有新版本,是否立即更新");

                                mDialog.setType(9);

                            }else{

                                mDialog.setTip("当前为最新版本");

                                mDialog.setType(0);

                            }

                            mDialog.show();

                        }


                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                    }
                }).execute(this);

                break;

            case R.id.ll_copyright:

                startActivity(new Intent(context,CopyrightHavingActivity.class));

                break;
        }

    }


    public abstract class UpdateSoftware{

        public abstract void update();

    }


}
